package org.robot.scm.pojos.platform.server;

import com.google.common.collect.Lists;
import java.time.LocalTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.robot.scm.platform.registry.TelegramRegistry;

/**
 * @Author : Awn create on 2024/5/7 15:22
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ExecuteParam {

    private String fileKey;
    private String script;
    private boolean mec02;
    private boolean mec03;
    private boolean mec04;
    private boolean mec06;
    private boolean mec15;
    private boolean mec16;
    private boolean mec17;
    private boolean mec18;
    private boolean mec19;
    private boolean mec20;
    private boolean mec21;
    private boolean mec22;
    private boolean mec23;
    private boolean mec24;
    private boolean mec25;
    private boolean mvd31;
    private boolean svd32;
    private boolean isAll;

    private int fileLine;
    private Boolean isCleanTask;
    private Boolean isShellExec;

    public List<String> initParams() {
        List<String> params = Lists.newArrayList();
        if (isMec02()) {
            params.add("mec02");
        }
        if (isMec03()) {
            params.add("mec03");
        }
        if (isMec04()) {
            params.add("mec04");
        }
        if (isMec06()) {
            params.add("mec06");
        }
        if (isMec15()) {
            params.add("mec15");
        }
        if (isMec16()) {
            params.add("mec16");
        }
        if (isMec17()) {
            params.add("mec17");
        }
        if (isMec18()) {
            params.add("mec18");
        }
        if (isMec19()) {
            params.add("mec19");
        }
        if (isMec20()) {
            params.add("mec20");
        }
        if (isMec21()) {
            params.add("mec21");
        }
        if (isMec22()) {
            params.add("mec22");
        }
        if (isMec23()) {
            params.add("mec23");
        }
        if (isMec24()) {
            params.add("mec24");
        }
        if (isMec25()) {
            params.add("mec25");
        }
        if (isMvd31()) {
            params.add("mvd31");
        }
        if (isSvd32()) {
            params.add("svd32");
        }
        return params;
    }

    /**
     * 检查是否可以清理
     */
    public void checkClean() {

        //【下午清理 0点～6点】15、16、17
        // 【凌晨清理 15点～22点】02、03、04、06、18
        LocalTime time = LocalTime.now();
        boolean isAfterMidnight = time.isAfter(LocalTime.MIDNIGHT) && time.isBefore(LocalTime.of(6, 0));

        List<String> params = initParams();
        List<String> searchMidParams = Lists.newArrayList("mec02", "mec03", "mec04", "mec06", "mec18");
        boolean containsAnyMid = params.stream().anyMatch(searchMidParams::contains);
        if (!isAfterMidnight && containsAnyMid) {
            throw new RuntimeException("2、3、4、6、18套环境0～6点执行");
        }

        boolean isAfterNoon = time.isAfter(LocalTime.of(15, 0)) && time.isBefore(LocalTime.of(22, 0));
        List<String> searchNoonParams = Lists.newArrayList("mec15", "mec16", "mec17");
        boolean containsAnyNoon = params.stream().anyMatch(searchNoonParams::contains);
        if (!isAfterNoon && containsAnyNoon) {
            throw new RuntimeException("15、16、17套环境15～21点执行");
        }
    }

    public ExecuteParam mec() {
        TelegramRegistry.likeEnvIds.forEach(envId -> {
            switch (envId) {
                case 2:
                    mec02 = true;
                    break;
                case 3:
                    mec03 = true;
                    break;
                case 4:
                    mec04 = true;
                    break;
                case 6:
                    mec06 = true;
                    break;
                case 15:
                    mec15 = true;
                    break;
                case 16:
                    mec16 = true;
                    break;
                case 17:
                    mec17 = true;
                    break;
                case 18:
                    mec18 = true;
                    break;
                case 19:
                    mec19 = true;
                    break;
                case 20:
                    mec20 = true;
                    break;
                case 21:
                    mec21 = true;
                    break;
                case 22:
                    mec22 = true;
                    break;
                case 23:
                    mec23 = true;
                    break;
                case 24:
                    mec24 = true;
                    break;
                case 25:
                    mec25 = true;
                    break;
                case 31:
                    mvd31 = true;
                    break;
                case 32:
                    svd32 = true;
                    break;
            }
        });
        return this;
    }
}
